package com.mexico.inloancash.dialog;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.mexico.inloancash.R;
import com.mexico.inloancash.adapter.IndiaBindAreaAdapter;
import com.mexico.inloancash.bean.India_Address_ListBean;
import com.mexico.inloancash.india_interfacedai.IndiaChooseAddressInterface;
import com.mexico.inloancash.utils.MyPreferencesManager;
import com.mexico.inloancash.utils.MyToast;

import java.util.ArrayList;
import java.util.List;

/**
 * 自己写的地区选择弹框，逻辑可能有点绕
 */
public class IndiaChoose_Address_Dialog extends Dialog implements BaseQuickAdapter.OnItemChildClickListener {
    private TextView sure;
    private TextView cancel;
    private TextView title;
    private IndiaChooseAddressInterface anInterface;
    private View line1;
    private View line2;
    private View line3;
    private TextView firstAddress;
    private TextView secondAddress;
    private TextView thirdAddress;
    private LinearLayout firstLinear;
    private LinearLayout secondLinear;
    private LinearLayout thirdLinear;
    private List<String> list1;//存放第一级地址
    private List<String> list2;//存放第二级地址
    private List<String> list3;//存放第三级地址
    private RecyclerView addressRecycle;
    private India_Address_ListBean indiaAddressListBean;
    private IndiaBindAreaAdapter indiaBindAreaAdapter;
    private int count = 1;//判断选择第几步
    private int posistion1;//第一步的item位置
    private int posistion2;//第二步的item位置
    private String first;//选择后第一级地址
    private String second;//选择后第二级地址
    private String third;//选择后第三级地址
    private String type;

     public IndiaChoose_Address_Dialog(@NonNull Context context, IndiaChooseAddressInterface anInterface, String type) {
         super(context,R.style.MyDialog);
        this.anInterface = anInterface;
        list1 = new ArrayList<>();
        list2 = new ArrayList<>();
        list3 = new ArrayList<>();
        indiaBindAreaAdapter = new IndiaBindAreaAdapter();
        indiaAddressListBean = new Gson().fromJson(MyPreferencesManager.getString("address",""), India_Address_ListBean.class);
        for (int i = 0; i < indiaAddressListBean.getData().size() ; i ++){
            list1.add(indiaAddressListBean.getData().get(i).getName());
        }
        this.type = type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.india_activity_choose_view_layout_address_dialog);
        Window win = getWindow();
        win.setGravity(Gravity.BOTTOM);
        WindowManager.LayoutParams lp = win.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        win.setAttributes(lp);
        setCanceledOnTouchOutside(false);
        init();
        deal();
    }
    public void init(){
        cancel = findViewById(R.id.cancel);
        sure = findViewById(R.id.sure);
        title = findViewById(R.id.title);
        firstAddress = findViewById(R.id.first_address);
        secondAddress = findViewById(R.id.second_address);
        thirdAddress = findViewById(R.id.third_address);
        line1 = findViewById(R.id.first_line);
        line2 = findViewById(R.id.second_line);
        line3 = findViewById(R.id.third_line);
        firstLinear = findViewById(R.id.first_linear);
        secondLinear = findViewById(R.id.second_linear);
        thirdLinear = findViewById(R.id.third_linear);
        addressRecycle = findViewById(R.id.address_recycle);
        indiaBindAreaAdapter.setOnItemChildClickListener(this);
        addressRecycle.setAdapter(indiaBindAreaAdapter);
        indiaBindAreaAdapter.setNewData(list1);
    }

    public void deal(){
        title.setText("Pilih Alamat");
        firstLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                indiaBindAreaAdapter.setNewData(list1);
                secondLinear.setVisibility(View.GONE);
                thirdLinear.setVisibility(View.GONE);
                first = "";
                second = "";
                third = "";
                firstAddress.setText("Silakan Pilih");
                line1.setVisibility(View.VISIBLE);
                count = 1;
            }
        });
        secondLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                indiaBindAreaAdapter.setNewData(list2);
                thirdLinear.setVisibility(View.GONE);
                second = "";
                third = "";
                secondAddress.setText("Silakan Pilih");
                line2.setVisibility(View.VISIBLE);
                count = 2;
            }
        });
        thirdLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                count = 3;
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });
        sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(!TextUtils.isEmpty(first) && !TextUtils.isEmpty(second) && !TextUtils.isEmpty(third)){
                    anInterface.choose(type,first+second+third);
                    dismiss();
                }else {
                    MyToast.show(getContext(),"Silakan Pilih Alamat Tertentu");
                }
            }
        });
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
         switch (view.getId()){
             case R.id.address_text:
                 if(count == 1){
                     posistion1 = position;
                     first = list1.get(position);
                     firstAddress.setText(list1.get(position));
                     line1.setVisibility(View.INVISIBLE);
                     secondLinear.setVisibility(View.VISIBLE);
                     line2.setVisibility(View.VISIBLE);
                     secondAddress.setText("Silakan Pilih");
                     list2.clear();
                     for(int i = 0; i < indiaAddressListBean.getData().get(position).getRegencies().size() ; i ++){
                         list2.add(indiaAddressListBean.getData().get(position).getRegencies().get(i).getName());
                     }
                     adapter.setNewData(list2);
                     count = 2;
                 }else if(count == 2){
                     posistion2 = position;
                     second = list2.get(position);
                     secondAddress.setText(list2.get(position));
                     line2.setVisibility(View.INVISIBLE);
                     thirdLinear.setVisibility(View.VISIBLE);
                     thirdAddress.setText("Silakan Pilih");
                     line3.setVisibility(View.VISIBLE);
                     list3.clear();
                     for(int i = 0; i < indiaAddressListBean.getData().get(posistion1).getRegencies().get(position).getDistricts().size() ; i ++){
                         list3.add(indiaAddressListBean.getData().get(posistion1).getRegencies().get(position).getDistricts().get(i).getName());
                     }
                     adapter.setNewData(list3);
                     count = 3;
                 }else {
                     third = list3.get(position);
                     thirdAddress.setText(list3.get(position));
                     line3.setVisibility(View.INVISIBLE);
                 }
                 break;
         }
    }
}
